package com.molyfun.parents.view;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.rest.bean.TypeTabListResponse;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by william(liuchao.hit@gmail.com).
 * 底部横条的TabBar
 */
public class ListBaselineTabBar extends RecyclerView {

    protected LayoutInflater layoutInflater;

    private int mChooseTab = 0;

    private Context context;

    LinearLayoutManager mLinearLayoutManager;
    private ListBaselineAdapter mListBaselineAdapter;

    private List<TypeTabListResponse.TypeTabBean> mTabsList;

    private ListBaselineItemClickListener listener;

    public ListBaselineTabBar(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public ListBaselineTabBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public ListBaselineTabBar(Context context) {
        super(context);
        init(context, null);
    }

    public void initDataList(List<TypeTabListResponse.TypeTabBean> list){
        this.mTabsList = list;
        mListBaselineAdapter.notifyDataSetChanged();
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        mLinearLayoutManager = new LinearLayoutManager(context);
        mLinearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        setLayoutManager(mLinearLayoutManager);
//        layoutInflater = LayoutInflater.from(context);
//        layoutInflater.inflate(R.layout.widget_attachment, this);
        setHasFixedSize(true);

        mListBaselineAdapter = new ListBaselineAdapter();
        setAdapter(mListBaselineAdapter);
    }

    public int getCurrentChooseTab(){
        return mChooseTab;
    }

    public void setListBaselineItemClickListener(ListBaselineItemClickListener listener) {
        this.listener = listener;
    }

    public static interface ListBaselineItemClickListener {
        void onBaselineItemClick(int position, TypeTabListResponse.TypeTabBean typeTabBean);
    }

    public class ListBaselineAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        public ListBaselineAdapter() {
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new ListBaselineViewHolder(LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.layout_list_baseline_cell, parent, false));
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
            ListBaselineViewHolder listBaselineViewHolder = (ListBaselineViewHolder)holder;
            listBaselineViewHolder.mBaseLineTabText.setText(mTabsList.get(position).getName());
            if(mChooseTab == position){
                listBaselineViewHolder.mBaselineLayout.setBackgroundResource(R.drawable.bg_baseline_tabbar_clicked);
                listBaselineViewHolder.mBaseLineTabText.setTextColor(ContextCompat.getColor(context, R.color.color_base_title_bar));
            }else{
                listBaselineViewHolder.mBaselineLayout.setBackgroundColor(ContextCompat.getColor(context, R.color.color_white));
                listBaselineViewHolder.mBaseLineTabText.setTextColor(ContextCompat.getColor(context, R.color.text_color_normal));
            }
        }

        @Override
        public int getItemCount() {
            if (mTabsList == null || mTabsList.size() == 0) {
                return 0;
            }
            return mTabsList.size();
        }

        public class ListBaselineViewHolder extends RecyclerView.ViewHolder implements OnClickListener{

            @InjectView(R.id.baseline_layout)
            RelativeLayout mBaselineLayout;
            @InjectView(R.id.baseline_tab_text)
            TextView mBaseLineTabText;

            public ListBaselineViewHolder(View view) {
                super(view);
                ButterKnife.inject(this, view);
                mBaselineLayout.setOnClickListener(this);
            }

            @Override
            public void onClick(View v) {
                switch (v.getId()){
                    case R.id.baseline_layout:
                        int clickNum = getAdapterPosition();
                        if(listener != null && clickNum >= 0){
                            listener.onBaselineItemClick(clickNum, mTabsList.get(clickNum));
                            mChooseTab = clickNum;
                            notifyDataSetChanged();
                        }
                        break;
                }
            }
        }
    }

}